#include "gotodialog.h"
#include "ui_gotodialog.h"

GoToDialog::GoToDialog(QWidget *parent) : QDialog(parent),
                                          ui(new Ui::GoToDialog)
{
    ui->setupUi(this);
    setWindowFlags(Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
    setWindowTitle("转到指定行");
}

GoToDialog::~GoToDialog()
{
    delete ui;
}

void GoToDialog::on_GoToButton_clicked()
{
    QString value = ui->lineEdit->text().trimmed();
    if (value.isEmpty())
    {
        this->showMessage("请输入要跳到的行数");
        return;
    }

    QRegularExpression rx("^([1-9]\\d*|0)$"); //正整数正则表达式
    QRegularExpressionMatch match = rx.match(value);
    if (!match.hasMatch())
    {
        this->showMessage("请输入正整数");
        return;
    }
    emit goToLine(value);
}

void GoToDialog::on_CancelButton_clicked()
{
    this->close();
}

void GoToDialog::receiveData(int value)
{
    ui->lineEdit->setText(QString::number(value));
}

void GoToDialog::showMessage(QString title)
{
    QMessageBox box(QMessageBox::Question, "记事本 - 跳行", title, QMessageBox::Ok);
    box.button(QMessageBox::Ok)->setText("确定");
    box.setIcon(QMessageBox::NoIcon);
    box.setWindowFlags(Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint);
    box.exec();
}
